package tpdisenio2009.mvp.display;

import java.util.List;
import javax.swing.table.AbstractTableModel;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.SoccerPlayer;

/**
 *
 * @author Nicolás Kreiff
 */
public class FantasyTeamPlayerTableModel extends AbstractTableModel {

    private final List<FantasyTeamPlayer> players;
    private static final int COLUMN_COUNT = 4;
    final String[] columnNames = {"Posición", "Jugador", "Club", "Cotización"};

    public FantasyTeamPlayerTableModel( List<FantasyTeamPlayer> players ) {
        this.players = players;
    }

    @Override
    public String getColumnName(int column) {
        if (( column < 0 ) || ( column > 3 ))
            return "";
        else
            return columnNames[column];
    }

    public FantasyTeamPlayer getFantasyTeamPlayer(int row) {
        if (row < 0 || row >= players.size())
            return null;
        FantasyTeamPlayer player = null;

        if (row == 0)
            player = getPlayerWithPositionAndRole(SoccerPlayer.GOALKEEPER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, 0);
        else if (row >= 1 && row <= 4)
            player = getPlayerWithPositionAndRole(SoccerPlayer.DEFENDER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, row - 1);
        else if (row >= 5 && row <= 7)
            player = getPlayerWithPositionAndRole(SoccerPlayer.MIDFIELDER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, row - 5);
        else if (row >= 8 && row <= 10)
            player = getPlayerWithPositionAndRole(SoccerPlayer.FORWARD_POSITION, FantasyTeamPlayer.TITULAR_ROLE, row - 8);
        else if (row == 11)
            player = getPlayerWithPositionAndRole(SoccerPlayer.GOALKEEPER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
        else if (row == 12)
            player = getPlayerWithPositionAndRole(SoccerPlayer.DEFENDER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
        else if (row == 13)
            player = getPlayerWithPositionAndRole(SoccerPlayer.MIDFIELDER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
        else if (row == 14)
            player = getPlayerWithPositionAndRole(SoccerPlayer.FORWARD_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
        return player;
    }

    public FantasyTeamPlayer getPlayerByRole( String role ) {
        if (role == null)
            return null;

        for (FantasyTeamPlayer player : players)
            if (role.equalsIgnoreCase( player.getRol() ))
                return player;

        return null;
    }

    public FantasyTeamPlayer getPlayerByRoleString( String role ) {
        if (role == null)
            return null;

        for (FantasyTeamPlayer player : players)
            if (role.equalsIgnoreCase( player.getSoccerPlayer().getPosition() + " " + player.getRol() ))
                return player;

        return null;
    }

    public void setPlayerByRole( String role, FantasyTeamPlayer player ) {
        if (role == null || player == null)
            return;

        for (int i=0; i<players.size(); i++)
            if (role.equalsIgnoreCase( players.get( i ).getRol() )) {
                players.remove( i );
                players.add( i, player );
                return;
            }
    }

    public int getRowCount() {
        return players.size();
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        /*
        try {
            FantasyTeamPlayer player = this.players.get(rowIndex);
            SoccerPlayer soccerPlayer = player.getSoccerPlayer();
            switch (columnIndex) {
                case 0:
                    return player.getSoccerPlayer().getPosition() + " " + player.getRol();
                case 1:
                    return soccerPlayer.getLastName() + ", " + soccerPlayer.getFirstName();
                case 2:
                    return player.getSoccerPlayer().getRealTeam().getName();
                case 3:
                    return soccerPlayer.getPrice();
            }
        } catch (Exception e) {
        }*/
        try {
            FantasyTeamPlayer player = null;

            if (rowIndex == 0)
                player = getPlayerWithPositionAndRole(SoccerPlayer.GOALKEEPER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, 0);
            else if (rowIndex >= 1 && rowIndex <= 4)
                player = getPlayerWithPositionAndRole(SoccerPlayer.DEFENDER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, rowIndex - 1);
            else if (rowIndex >= 5 && rowIndex <= 7)
                player = getPlayerWithPositionAndRole(SoccerPlayer.MIDFIELDER_POSITION, FantasyTeamPlayer.TITULAR_ROLE, rowIndex - 5);
            else if (rowIndex >= 8 && rowIndex <= 10)
                player = getPlayerWithPositionAndRole(SoccerPlayer.FORWARD_POSITION, FantasyTeamPlayer.TITULAR_ROLE, rowIndex - 8);
            else if (rowIndex == 11)
                player = getPlayerWithPositionAndRole(SoccerPlayer.GOALKEEPER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
            else if (rowIndex == 12)
                player = getPlayerWithPositionAndRole(SoccerPlayer.DEFENDER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
            else if (rowIndex == 13)
                player = getPlayerWithPositionAndRole(SoccerPlayer.MIDFIELDER_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);
            else if (rowIndex == 14)
                player = getPlayerWithPositionAndRole(SoccerPlayer.FORWARD_POSITION, FantasyTeamPlayer.SUBSTITUTE_ROLE, 0);

            SoccerPlayer soccerPlayer = player.getSoccerPlayer();
            switch (columnIndex) {
                case 0:
                    return player.getSoccerPlayer().getPosition() + " " + player.getRol();
                case 1:
                    return soccerPlayer.getLastName() + ", " + soccerPlayer.getFirstName();
                case 2:
                    return player.getSoccerPlayer().getRealTeam().getName();
                case 3:
                    return soccerPlayer.getPrice();
            }
        } catch (Exception e) {
        }

        return null;
    }


    private FantasyTeamPlayer getPlayerWithPositionAndRole( String pos, String role, int nro ) {
        for (FantasyTeamPlayer player : players) {
            if (pos.equalsIgnoreCase( player.getSoccerPlayer().getPosition() ) && role.equalsIgnoreCase( player.getRol() )) {
                if (nro-- == 0)
                    return player;
            }
        }
        return null;
    }
}
